package com.hns.dev.linys.file;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.text.*;
import java.util.concurrent.atomic.AtomicInteger;


public class HandleCanData {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //private static final String FILE_DIR_PATH = "D:\\tmp";
    private static final String CAN_STRING = "CAN:%s(";
    private static final String GPS_STRING = "GPS:%s(";
    //private static final String[] carNos = {"闽D03205D"};
    private static final Map<String, AtomicInteger> carsMap = new HashMap<>();

    public static void main(String[] args) throws Exception{

        URL resourceUrl = HandleCanData.class.getResource("");
        System.out.println("运行目录：" + resourceUrl.getPath());
        String path = resourceUrl.getPath();

        File f = new File(path + "\\conf.txt");
        BufferedReader reader1 = new BufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(f),"GB2312")));
        String readFilesPath = reader1.readLine();
        String carNoStr = reader1.readLine();
        String[] carNos = carNoStr.split(",");
        String outPutTxt = reader1.readLine();

        File outPut = new File(path + "\\" + outPutTxt);
        if (!outPut.exists()) {
            outPut.createNewFile();
        } else {
            outPut.delete();
            outPut.createNewFile();
        }

        File readFiles = new File(readFilesPath);
        File[] files = readFiles.listFiles();
        BufferedReader reader = null;
        BufferedWriter writer = new BufferedWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPut),"GB2312")));

        for (String carNo: carNos) {
            carsMap.put(carNo, new AtomicInteger(0));
        }

        for(File file: files) {
            if(file.getName().startsWith("gps_data")){
                reader = new BufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(file),"GB2312")));
                System.out.println("开始读取文件：" + file.getName() + ".........." + sdf.format(new Date()));
                writer.write("开始读取文件：" + file.getName() + ".........." + sdf.format(new Date()));
                writer.newLine();
                String lineStr = null;
                while ((lineStr = reader.readLine()) != null) {
                    for (String carNo: carNos) {
                        if (lineStr.contains(String.format(CAN_STRING, carNo))) {
                            AtomicInteger ai = carsMap.get(carNo);
                            ai.incrementAndGet();
                            carsMap.put(carNo, ai);
                        }
                    }
                }
                for(String key: carNos) {
                    System.out.println(String.format(CAN_STRING, key) + "---->>>>count=" + carsMap.get(key).get());
                    writer.write(String.format(CAN_STRING, key) + "---->>>>count=" + carsMap.get(key).get());
                    writer.newLine();
                    carsMap.put(key, new AtomicInteger(0));
                }
                System.out.println("结束读取文件：" + file.getName() + "............" + sdf.format(new Date()));
                System.out.println();
                writer.write("结束读取文件：" + file.getName() + "............" + sdf.format(new Date()));
                writer.newLine();
                writer.newLine();
                writer.flush();
            }
        }

        if (reader1 != null) {
            reader1.close();
        }

        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }

    }

}
